package com.cfx.exercise_42;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class PDF2  {

	Document doc = new Document();// 建立一个Document对象
	private ByteArrayOutputStream ba;

	// private static Font headfont;// 设置字体大小
	private static Font titleFont;// 设置字体大小
	private static Font textFont;// 设置字体大小
	private static Font keyFont;
	int maxWidth = 520;

	public ByteArrayOutputStream getBa() {
		return ba;
	}

	public void setBa(ByteArrayOutputStream ba) {
		this.ba = ba;
	}

	/**
	 * 静态代码块
	 */
	static {
		BaseFont bfChinese;
		try {
			// bfChinese =
			// BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
			bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
					BaseFont.NOT_EMBEDDED, true);
			// headfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小
			titleFont = new Font(bfChinese, 20, Font.BOLD);// 设置字体大小
			textFont = new Font(bfChinese, 12, Font.NORMAL);// 设置字体大小
			keyFont = new Font(bfChinese, 13, Font.NORMAL);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 构造方法
	 * 
	 * @param file
	 */
	public PDF2(File file) {
		doc.setPageSize(PageSize.A4);// 设置页面大小
		try {
			PdfWriter.getInstance(doc, new FileOutputStream(file));
			doc.open();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public PDF2() {
		ba = new ByteArrayOutputStream();
		doc.setPageSize(PageSize.A4);// 设置页面大小
		doc.setMargins(50, 50, 50, 50);
		try {
			PdfWriter.getInstance(doc, ba);
			doc.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param code 批單編碼
	 * @param time 审批时间
	 * @param companName 企业名称
	 * @param leaderName 企业法人
	 * @param agentName 经办人姓名
	 * @param agentPhone 经办人手机
	 * @param count 印章数量
	 * @param policeName 审批单位
	 * @param activePwd 承接密码
	 * @param sealList 印章列表
	 * @param factoryList 企业列表
	 * @throws Exception
	 */
	public void getKFXPDF(String code, String time, String companName,
			String leaderName, String agentName, String agentPhone,
			String count, String policeName, String activePwd,
			List<String[]> sealList,List<String[]> factoryList) throws Exception {
		Paragraph title = new Paragraph("黑河市公安局印章准刻通知书", titleFont);
		title.setAlignment(Element.ALIGN_CENTER);// 设置居中
		PdfPTable table = new PdfPTable(1);// 外table
		PdfPCell cell = new PdfPCell();
		cell.setBorderWidth(1f);
		table.setWidthPercentage(100f);
		table.setSplitLate(false);// 自动换页
		PdfPTable pt = new PdfPTable(2);
		pt.setSpacingBefore(40f);
		pt.setWidthPercentage(100f);
		pt.setWidths(new float[] { 50f, 50f });
		PdfPCell c1 = new PdfPCell(new Paragraph("批单编号：" + code, keyFont));
		c1.setBorder(0);
		c1.setHorizontalAlignment(Element.ALIGN_LEFT);
		PdfPCell c2 = new PdfPCell(new Paragraph("审批日期：" + time, keyFont));
		c2.setBorder(0);
		c2.setHorizontalAlignment(Element.ALIGN_RIGHT);
		pt.addCell(c1);
		pt.addCell(c2);
		// codeAndTime.setLeading(50f);
		// ///////////////////////////////
		PdfPTable t1 = new PdfPTable(3);
		t1.setWidthPercentage(100f);
		t1.setWidths(new float[] { 24f, 24f, 24f });
		PdfPCell t1c1 = new PdfPCell(new Paragraph("申刻单位：" + companName,
				keyFont));
		t1c1.setColspan(3);
		t1c1.setHorizontalAlignment(Element.ALIGN_LEFT);
		t1c1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c1.setFixedHeight(20f);
		t1c1.setBorder(0);
		PdfPCell t1c2 = new PdfPCell(new Paragraph("法定代表：" + leaderName,
				keyFont));
		t1c2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c2.setHorizontalAlignment(Element.ALIGN_LEFT);
		t1c2.setFixedHeight(20f);
		t1c2.setBorder(3);
		PdfPCell t1c3 = new PdfPCell(new Paragraph("经办人：" + agentName, keyFont));
		t1c3.setHorizontalAlignment(Element.ALIGN_LEFT);
		t1c3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c3.setBorder(3);
		PdfPCell t1c4 = new PdfPCell(new Paragraph("联系电话：" + agentPhone,
				keyFont));
		t1c4.setHorizontalAlignment(Element.ALIGN_LEFT);
		t1c4.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t1c4.setBorder(3);
		t1.addCell(t1c1);
		t1.addCell(t1c2);
		t1.addCell(t1c3);
		t1.addCell(t1c4);
		// ///////////////////////////
		PdfPTable t2 = new PdfPTable(4);
		t2.setWidthPercentage(100f);
		t2.setWidths(new float[] { 3f, 35f, 15f, 10f });
		PdfPCell t2c1 = new PdfPCell(new Paragraph(
				"批\n\n准\n\n刻\n\n制\n\n印\n\n章", keyFont));
		t2c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		t2c1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t2c1.setRowspan(sealList.size() + 2);
		t2c1.setBorder(8);
		PdfPCell t2c2 = new PdfPCell(new Paragraph("印章全称", keyFont));
		t2c2.setHorizontalAlignment(Element.ALIGN_CENTER);
		t2c2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t2c2.setFixedHeight(20f);
		t2c2.setBorder(6);
		// t2c2.setHorizontalAlignment(Element.ALIGN_MIDDLE);
		PdfPCell t2c3 = new PdfPCell(new Paragraph("印章类型", keyFont));
		t2c3.setHorizontalAlignment(Element.ALIGN_CENTER);
		t2c3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t2c3.setBorder(6);
		// t2c3.setHorizontalAlignment(Element.ALIGN_MIDDLE);
		PdfPCell t2c4 = new PdfPCell(new Paragraph("印章规格", keyFont));
		t2c4.setHorizontalAlignment(Element.ALIGN_CENTER);
		t2c4.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		t2c4.setBorder(6);
		// t2c4.setHorizontalAlignment(Element.ALIGN_MIDDLE);
		t2.addCell(t2c1);
		t2.addCell(t2c2);
		t2.addCell(t2c3);
		t2.addCell(t2c4);

		int k = 0;
		for (String[] strings : sealList) {
			k++;
			for (int i = 0; i < strings.length; i++) {
				PdfPCell pc = new PdfPCell(new Paragraph(strings[i], keyFont));
				pc.setHorizontalAlignment(Element.ALIGN_CENTER);
				pc.setFixedHeight(20f);
				pc.setBorder(1);
				if (k == sealList.size()) {
					switch (k) {
					case 1:
						pc.setFixedHeight(300);
						break;
					case 2:
						pc.setFixedHeight(280);
						break;
					case 3:
						pc.setFixedHeight(260);
						break;
					case 4:
						pc.setFixedHeight(240);
						break;
					case 5:
						pc.setFixedHeight(220);
						break;
					case 6:
						pc.setFixedHeight(200);
						break;
					case 7:
						pc.setFixedHeight(180);
						break;
					case 8:
						pc.setFixedHeight(160);
						break;
					case 9:
						pc.setFixedHeight(140);
						break;
					case 10:
						pc.setFixedHeight(120);
						break;
					case 11:
						pc.setFixedHeight(100);
						break;
					case 12:
						pc.setFixedHeight(80);
						break;
					case 13:
						pc.setFixedHeight(60);
						break;
					case 14:
						pc.setFixedHeight(40);
						break;
					case 15:
						pc.setFixedHeight(20);
						break;
					default:
						break;
					}
				}
				t2.addCell(pc);
			}
		}

		PdfPTable t3 = new PdfPTable(2);
		t3.setWidthPercentage(100f);
		t3.setWidths(new float[] { 20f, 20f });
		PdfPCell t3c1 = new PdfPCell(new Paragraph("经办人签名：", keyFont));
		t3c1.setHorizontalAlignment(Element.ALIGN_LEFT);
		t3c1.setBorder(1);
		t3c1.setFixedHeight(40f);
		PdfPCell t3c2 = new PdfPCell(new Paragraph("审批单位（盖章有效）：" + policeName,
				keyFont));
		t3c2.setHorizontalAlignment(Element.ALIGN_LEFT);
		t3c2.setBorder(1);
		PdfPCell t3c3 = new PdfPCell(
				new Paragraph("承接密码：" + activePwd, keyFont));
		t3c3.setHorizontalAlignment(Element.ALIGN_LEFT);
		t3c3.setBorder(0);
		t3c3.setFixedHeight(40f);
		PdfPCell t3c4 = new PdfPCell(new Paragraph("审批人签名：", keyFont));
		t3c4.setHorizontalAlignment(Element.ALIGN_LEFT);
		t3c4.setBorder(0);

		t3.addCell(t3c1);
		t3.addCell(t3c2);
		t3.addCell(t3c3);
		t3.addCell(t3c4);
		cell.addElement(t1);
		cell.addElement(t2);
		cell.addElement(t3);
		table.addCell(cell);
		Paragraph text = new Paragraph(
				"\n注意事项：\n1.\t批单打印后，需审批单位盖章有效，有效期为5个工作日，请在"
						+ "有效期内持有该通知书到黑河市有资质的公章\t   刻制企业进行印章刻制，逾期须重新申请；\n"
						+ "2.\t公章刻制企业将印章信息（印章编码、章面内容、审批地点等公开信息及加密信息）导入印章章体内的防伪\t   芯片"
						+ "，芯片的内容可以用专门读写设备读取\n\n", textFont);
		PdfPTable t = new PdfPTable(3);
		float[] width = { 10f, 10f, 10f };
		t.setWidths(width);
		t.setWidthPercentage(100);
		/*for (int i = 0; i < 3; i++) {
			Paragraph p1 = new Paragraph("企业名称：", textFont);
			p1.setAlignment(0);
			PdfPCell ca = new PdfPCell(p1);
			ca.setHorizontalAlignment(Element.ALIGN_LEFT);
			ca.setVerticalAlignment(Element.ALIGN_CENTER);
			ca.setBorder(0);
			PdfPCell cb = new PdfPCell(new Paragraph("企业地址：", textFont));
			cb.setBorder(0);
			PdfPCell cc = new PdfPCell(new Paragraph("企业电话：", textFont));
			cc.setBorder(0);
			t.addCell(ca);
			t.addCell(cb);
			t.addCell(cc);
		}*/
		for (String[] strings : factoryList) {
			for (int i = 0; i < strings.length; i++) {
				PdfPCell pc;
				if(i==0){
					pc = new PdfPCell(new Paragraph("企业名称："+strings[i],keyFont));
				}else if(i==1){
					pc = new PdfPCell(new Paragraph("企业地址："+strings[i],keyFont));
				}else{
					pc = new PdfPCell(new Paragraph("企业电话："+strings[i],keyFont));
				}
				
				pc.setHorizontalAlignment(Element.ALIGN_LEFT);
				pc.setVerticalAlignment(Element.ALIGN_CENTER);
				//pc1.setFixedHeight(20f);
				pc.setBorder(0);
				t.addCell(pc);
			}
		}
		doc.add(title);
		doc.add(pt);
		doc.add(table);
		doc.add(text);
		doc.add(t);
		doc.close();
	}
}
